package com.chencq.core.service.base.impl;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.chencq.core.dao.BaseDao;
import com.chencq.core.entity.Pager;
import com.chencq.core.service.base.BaseService;
import com.chencq.core.utils.beanutil.ReflectHelper;

/**
 * 基础业务逻辑
 * 
 * @param <T>
 */

public class BaseServiceImpl<T> implements BaseService<T> {

	@Autowired
	private BaseDao<T> baseDao;

	/**
	 * 获取实体对象
	 */
	private Class<T> getEntity() {
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		return clazz;
	}

	/**
	 * 获取实体对象的名称
	 */
	private String getEntityName() {
		return getEntity().getSimpleName();
	}

	@Override
	public String save(T o) {
		if (null == o) {
			throw new NullPointerException("entity bean is null");
		}
		return baseDao.save(o);
	}

	@Override
	public void delete(T o) {
		baseDao.delete(o);
	}

	@Override
	public void update(T o) {
		if (null == o) {
			throw new NullPointerException("entity bean is null");
		}
		baseDao.update(o);
	}

	@Override
	public void saveOrUpdate(T o) {
		baseDao.saveOrUpdate(o);
	}

	@Override
	public T getById(String id) {
		Class<T> c = getEntity();
		return baseDao.getById(c, id);
	}

	@Override
	public Long countHQL(T o) {
		return baseDao.count("SELECT count(*) " + whereHQL(o), getParams(o));
	}

	@Override
	public List<T> findAll(T o) {
		return baseDao.find(whereHQL(o), getParams(o));
	}

	@Override
	public Pager findPageList(T o, Integer pageNo, Integer pageSize) {
		Long total = countHQL(o);
		Pager pager = new Pager(pageNo, pageSize, total.intValue());
		if (total > 0) {
			List<T> list = baseDao.find(whereHQL(o), getParams(o));
			pager.setDatas(list);
		}
		return pager;
	}

	private String whereHQL(T o) {
		String className = getEntityName();
		StringBuilder builder = new StringBuilder();
		builder.append("FROM  ").append(className).append(" t");
		if (o != null) {
			builder.append(" WHERE 1=1 ");
			// 获取实体所有的字段
			Field[] field = ReflectHelper.getAllField(o);
			for (Field f : field) {
				String fieldName = f.getName();
				Object fieldValue = null;
				try {
					fieldValue = ReflectHelper
							.getValueByFieldName(o, fieldName);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (null != fieldValue && fieldValue != "") {
					builder.append(" AND ").append("t.").append(fieldName)
							.append("=:").append(fieldName);
				}
			}
		}
		return builder.toString();
	}

	private Map<String, Object> getParams(T obj) {
		if (obj == null) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		Field[] field = ReflectHelper.getAllField(obj);
		for (Field f : field) {
			String fieldName = f.getName();
			Object fieldValue = null;
			try {
				fieldValue = ReflectHelper.getValueByFieldName(obj, fieldName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (null != fieldValue && fieldValue != "") {
				params.put(fieldName, fieldValue);
			}
		}
		return params;
	}
}